//
//  LYProgressHUD.m
//  YiHua
//
//  Created by Hero on 2017/9/22.
//  Copyright © 2017年 Hero. All rights reserved.
//

#import "LYProgressHUD.h"

// 背景视图的宽度/高度
#define BGVIEW_WIDTH 100.0f
// 文字大小
#define TEXT_SIZE    16.0f

@implementation LYProgressHUD

+ (instancetype)sharedHUD {
    static id hud;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[self alloc] initWithView:[UIApplication sharedApplication].keyWindow];
    });
    return hud;
}

+ (void)showStatus:(LYProgressHUDStatus)status text:(NSString *)text {
    
    LYProgressHUD *HUD = [LYProgressHUD sharedHUD];
    HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    HUD.bezelView.color = [UIColor colorWithRed:77/255.0 green:77/255.0 blue:77/255.0 alpha:1];
    HUD.contentColor=[UIColor whiteColor];
    [HUD showAnimated:YES];
    [HUD setShowNow:YES];
    //蒙版显示 YES , NO 不显示
    //        HUD.dimBackground = YES;
    HUD.label.text = text;
    HUD.label.textColor = [UIColor whiteColor];
    [HUD setRemoveFromSuperViewOnHide:YES];
    HUD.label.font = [UIFont boldSystemFontOfSize:TEXT_SIZE];
    [HUD setMinSize:CGSizeMake(BGVIEW_WIDTH, BGVIEW_WIDTH)];
    [[UIApplication sharedApplication].keyWindow addSubview:HUD];
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"LYProgressHUD" ofType:@"bundle"];
    switch (status) {

        case LYProgressHUDStatusSuccess: {
            
            NSString *sucPath = [bundlePath stringByAppendingPathComponent:@"success.png"];
            UIImage *sucImage = [UIImage imageWithContentsOfFile:sucPath];
            
            HUD.mode = MBProgressHUDModeCustomView;
            UIImageView *sucView = [[UIImageView alloc] initWithImage:sucImage];
            HUD.customView = sucView;
//            [HUD hideAnimated:YES afterDelay:2.0f];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [HUD setShowNow:NO];
                [[LYProgressHUD sharedHUD] hideAnimated:YES];
            });
        }
            break;
            
        case LYProgressHUDStatusError: {
            
            NSString *errPath = [bundlePath stringByAppendingPathComponent:@"error.png"];
            UIImage *errImage = [UIImage imageWithContentsOfFile:errPath];
            
            HUD.mode = MBProgressHUDModeCustomView;
            UIImageView *errView = [[UIImageView alloc] initWithImage:errImage];
            HUD.customView = errView;
//            [HUD hideAnimated:YES afterDelay:2.0f];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [HUD setShowNow:NO];
                [[LYProgressHUD sharedHUD] hideAnimated:YES];

            });
        }
            break;
            
        case LYProgressHUDStatusLoading: {
            
            HUD.mode = MBProgressHUDModeIndeterminate;
        }
            break;
 
        case LYProgressHUDStatusWarn: {
            NSString *infoPath = [bundlePath stringByAppendingPathComponent:@"warn.png"];
            UIImage *infoImage = [UIImage imageWithContentsOfFile:infoPath];
            
            HUD.mode = MBProgressHUDModeCustomView;
            UIImageView *infoView = [[UIImageView alloc] initWithImage:infoImage];
            HUD.customView = infoView;
//            [HUD hideAnimated:YES afterDelay:2.0f];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [HUD setShowNow:NO];
                [[LYProgressHUD sharedHUD] hideAnimated:YES];
            });
            
        }
            break;
            
        case LYProgressHUDStatusInfo: {
            
            NSString *infoPath = [bundlePath stringByAppendingPathComponent:@"smile.png"];
            UIImage *infoImage = [UIImage imageWithContentsOfFile:infoPath];
            
            HUD.mode = MBProgressHUDModeCustomView;
            UIImageView *infoView = [[UIImageView alloc] initWithImage:infoImage];
            HUD.customView = infoView;
//            [HUD hideAnimated:YES afterDelay:2.0f];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [HUD setShowNow:NO];
                [[LYProgressHUD sharedHUD] hideAnimated:YES];
            });
        }
            break;
        case LYProgressHUDStatusAnimationLoading: {
            
            NSMutableArray *imageArr = [[NSMutableArray alloc] init];
            for (int i=1; i<=3; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ico_searching%d",i]];
                [imageArr addObject:image];
            }
            UIImageView *infoView = [[UIImageView alloc] init];
            infoView.animationImages = imageArr;
            infoView.animationDuration = 1;
            infoView.animationRepeatCount = 0;
            [infoView startAnimating];
            HUD.customView = infoView;
            HUD.mode = MBProgressHUDModeCustomView;
            
        }
            break;
            
        default:
            break;
    }
}

+ (void)showMessage:(NSString *)text {
    
    LYProgressHUD *HUD = [LYProgressHUD sharedHUD];
    HUD.bezelView.color = [UIColor blackColor];
    [HUD showAnimated:YES];
    [HUD setShowNow:YES];
    HUD.label.text = text;
    HUD.label.numberOfLines = 0;
    HUD.contentColor=[UIColor whiteColor];
    [HUD setMinSize:CGSizeZero];
    [HUD setMode:MBProgressHUDModeText];
    //    HUD.dimBackground = YES;
    [HUD setRemoveFromSuperViewOnHide:YES];
    HUD.label.font = [UIFont boldSystemFontOfSize:TEXT_SIZE];
    [[UIApplication sharedApplication].keyWindow addSubview:HUD];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[LYProgressHUD sharedHUD] setShowNow:NO];
        [[LYProgressHUD sharedHUD] hideAnimated:YES];
    });
}

+ (void)showWarn:(NSString *)text {
    
    [self showStatus:LYProgressHUDStatusWarn text:text];
}

+ (void)showError:(NSString *)text {
    
    [self showStatus:LYProgressHUDStatusError text:text];
}

+ (void)showSuccess:(NSString *)text {
    
    [self showStatus:LYProgressHUDStatusSuccess text:text];
}

+ (void)showLoading:(NSString *)text {
    
    [self showStatus:LYProgressHUDStatusLoading text:text];
}

+ (void)showAnimationLoading:(NSString *)text {
    
    [self showStatus:LYProgressHUDStatusAnimationLoading text:text];
}

+ (void)hideHUD {
    
    [[LYProgressHUD sharedHUD] setShowNow:NO];
    [[LYProgressHUD sharedHUD] hideAnimated:YES];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
